<?php
/**
 * 文章列表
*/
class ListAction extends HomeBaseAction {
	/**
	 * 分类列表页
	 * @return [type] [description]
	 */
	public function index() {
		if(I('get.location')){
			$this->location = I('get.location');
		}else{
			$this->location = I('get.id');
		}

		if(I('get.id')){
			$term_id = I('get.id');
			$term = M('terms')->where('term_id='.$term_id)->find();
		}elseif(I('get.url_name')){
			$url_name = I('get.url_name');
			$term = M('terms')->where("url_name = '".I('get.url_name')."'")->find();
			$term_id = $term['term_id'];
		}

		
		// 不存在该分类
		if(empty($term)) { header("HTTP/1.1 404 Not Found");exit; }

        $parents = get_parents($term_id);
        // pe($parents);
        $this->assign('parents',$parents);

		// 栏目点击数加一
		M('terms')->where('term_id='.$term_id)->setInc('count');

		// 显示模板 
		// 获取父分类模板 以父分类模板为主
		// if($term['parent'] != 0){
		// 	$tpl = M('terms')->where('term_id='.$term['parent'])->getField('list_tpl');
		// }else{
		// 	$tpl = $term['list_tpl']?$term['list_tpl']:'list';
		// }
		$tpl = $term['list_tpl']?$term['list_tpl']:'list';

		$this->assign('term',$term);
		// pe($term);
		/**
		 * 获取该分类下的所有子分类
		 */

		require  'Class/Unlimited.class.php';

		$category_list = M('terms')
			->order('listorder')
			->select();

		$child_list = Unlimited::getChild($category_list,$term_id,'term_id','parent');

		// p($child_list);
		// 分页

		import('Util.Page');

		$nowPage = isset($_GET['p'])?$_GET['p']:1;

		$num = $_GET['num']?$_GET['num']:10;

		if(empty($child_list)){

			// 没有子分类
			$count = M('posts')
				->where("term_id = $term_id and status <> 0")
				->count();
 			$Page       = new Page($count,$num);// 实例化分页类 传入总记录数
 			$Page->config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %upPage% %first%  %prePage%  %linkPage%  %nextPage% %end% %downPage%');
 			// $Page->url = 'list/'.$term_id.'/p';
 			// $Page->url = $term['taxonomy'].'/'.$term['url_name'].'/p';
 			// 
 			$Page->url = 'article/list/'.$term_id.'/p';
 			$lists = M()
 				->table("tc_terms t,tc_posts p")
 				->where("p.term_id = $term_id and t.term_id = p.term_id and p.status <> 0")
 				->order('p.post_date DESC')
 				->page($nowPage.','.$Page->listRows)
				->select();
    		$show       = $Page->show();// 分页显示输出

		}else{

			// echo 1;
			// 有子分类

			foreach ($child_list as $key => $value) {

				$terms[] = $value['term_id'];

			}

			$terms_str = implode(',', $terms);
			// echo $terms_str;
			$count      = M('posts')
				->where("term_id in ($terms_str) and status <> 0")
				->count();
			// echo $count;
 			$Page       = new Page($count,$num);// 实例化分页类 传入总记录数
 			$Page->config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %upPage% %first%  %prePage%  %linkPage%  %nextPage% %end% %downPage%');
 			// $Page->url = 'list/'.$term_id.'/p';
 			// $Page->url = $term['taxonomy'].'/'.$term['url_name'].'/p';
 			// 
 			$Page->url = 'article/list/'.$term_id.'/p';
 			$lists = M()
 				->table("tc_terms t,tc_posts p")
 				->where("p.term_id in ($terms_str) and t.term_id = p.term_id and p.status <> 0")
 				->order('p.post_date DESC')
 				->page($nowPage.','.$Page->listRows)
				->select();
			// echo M()->getLastSql();

			$show       = $Page->show();// 分页显示输出

		}

		// 无文章

		if(empty($lists)) {
			$this->no_lists = 1;
		}
		else{
			foreach ($lists as $key => $value) {

				// $url_name = M('terms')->where('term_id = '.$value['term_id'])->getField('url_name');
				// $taxonomy = M('terms')->where('term_id = '.$value['term_id'])->getField('taxonomy');
				// $lists[$key]['href'] = U("$taxonomy/$url_name".'/'.$value['ID']);
				$lists[$key]['images'] = json_decode($value['smeta'],1);
			}
			$this->lists = $lists;
		}
		// p($lists);
    	$this->page  = $show; // 赋值分页输出

  
		$this->display(":$tpl");


	}

	public function nav_index(){

		$navcatname="文章分类";

		$datas=sp_get_terms("field:term_id,name");

		$navrule=array(

				"action"=>"List/index",

				"param"=>array(

						"id"=>"term_id"

				),

				"label"=>"name");

		echo sp_get_nav4admin($navcatname,$datas,$navrule);

		

	}
	public function sql(){

		// M('terms')->where('parent = 45')->setField('list_tpl','product_list');
		// M('terms')->where('parent = 45')->setField('one_tpl','product');

	}

	

}